<template>
    <div v-loading="loading" element-loading-text="拼命加载中">
        <el-row>
            <!-- 按钮区 -->
            <el-col :span="24">
                <div class="button-wrapper">
                    <el-button size="mini" type="primary" round @click="handleAdd()">增加</el-button>
                    <el-button size="mini" type="danger" round @click="batchEdit(-1)">删除</el-button>
                </div>
            </el-col>
            <div class="search-box">
                <el-form :inline="true" :model="listQuery"  class="demo-form-inline">
                    <el-form-item label="部件类型：">
                        <el-select v-model="listQuery.partType" placeholder="请选择" clearable style="width:130px"
                                   class="filter-item">
                            <el-option v-for="item in partType" :key="item.dictId" :label="item.dictName" :value="item.dictValue"/>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="所属街道：">
                        <el-input v-model="listQuery.belongStreet" placeholder="所属街道"></el-input>
                    </el-form-item>

                    <el-form-item>
                        <el-button round size="mini" type="primary" @click="getList()">查询</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-row>
        <!-- 表格 -->
        <el-row>
            <el-col :span="24">
                <div class="data-wrapper">
                    <template v-if="tableData!=null">
                        <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%"
                                  @selection-change="handleSelectionChange">
                            <el-table-column type="selection" label="全选" width="40">
                            </el-table-column>
                            <el-table-column type="index" label="序号" width="50" :index="indexMethod">
                            </el-table-column>
                            <el-table-column prop="partsCode" label="部件编码" width="100">
                            </el-table-column>
                            <el-table-column prop="partsName" label="部件名称" width="100">
                            </el-table-column>
                            <el-table-column prop="partsType.name" label="部件类型" width="110">
                            </el-table-column>
                            <el-table-column prop="partsFlow" label="所属街道" width="110">
                            </el-table-column>
                            </el-table-column>
                            <el-table-column prop="regId.name" label="所属区域" width="110">
                            </el-table-column>
                            <el-table-column prop="riverId.name" label="处于河流段" width="100">
                            </el-table-column>
                            <el-table-column label="经纬度" width="90">
                                <template slot-scope="scope">
                                    <span>{{ scope.row.longitude}}/{{ scope.row.latitude}}</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="" label="GIS地图" width="90">
                            </el-table-column>
                            <el-table-column fixed="right" label="操作" align="center" >
                                <template slot-scope="scope">
                                    <el-button title="查看" size="small" icon="el-icon-search" type="primary" circle
                                               @click="handleView(scope.$index, scope.row)"></el-button>
                                    <el-button title="编辑" size="small" icon="el-icon-edit" type="success" circle
                                               @click="handleEdit(scope.$index, scope.row)"></el-button>
                                    <el-button title="删除" size="small" icon="el-icon-delete" type="danger" circle
                                               @click="handleDel(scope.$index, scope.row)"></el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </template>
                </div>
            </el-col>
        </el-row>
        <!-- 底部按钮和分页 -->
        <el-row :gutter="20" v-if="dataCount" type="flex" justify="space-between" align="center" class="page-wrapper">
            <el-col :span="20">
                <div class="page-item">
                    <el-pagination background
                                   @size-change="handleSizeChange"
                                   @current-change="handleCurrentChange"
                                   :current-page.sync="listQuery.page"
                                   :page-sizes="pageSizes"
                                   :page-size="listQuery.limit"
                                   layout="total, sizes, prev, pager, next, jumper"
                                   :total="dataCount"/>
                </div>
            </el-col>
        </el-row>
        <!-- 弹出窗口区域 -->
        <el-dialog title="河道部件信息" :visible.sync="dialogFormVisible"  @close="partsCancel('form')">
            <el-form ref="form" :model="form">
                <el-row :gutter="15">
                    <el-col :span="12">
                        <el-form-item label="部件名称" :label-width="formLabelWidth" prop="partsName">
                            <el-input v-model="form.partsName" auto-complete="off"  :readonly="dis"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="部件编码" :label-width="formLabelWidth" prop="partsCode">
                            <el-input v-model="form.partsCode" auto-complete="off" :readonly="dis" ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="15">
                    <el-col :span="12">
                        <el-form-item label="部件类型" :label-width="formLabelWidth" prop="partsType">
                            <el-select v-model="form.partsType" placeholder="请选择" clearable style="width:130px"
                                       class="filter-item"  :readonly="dis">
                                <el-option v-for="item in partType" :key="item.dictId" :label="item.dictName" :value="item.dictValue"/>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="12">
                        <el-form-item label="所属街道" :label-width="formLabelWidth" prop="partsFlow">
                            <el-input v-model=" form.partsFlow" placeholder="所属街道"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="5">
                    <el-col :span="12">
                        <el-form-item label="所属区域" :label-width="formLabelWidth" prop="selectedAddress">
                            <el-cascader class="width-all"
                                         :options="treeData"
                                         :props="addProps"
                                         :change-on-select="true"
                                         v-model="form.selectedAddress"
                                         @change="handleChange"></el-cascader>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="所处河段" :label-width="formLabelWidth" prop="riverId">
                            <el-select v-model="form.riverId" placeholder="请选择" v-bind:disabled="disabledInput"  clearable style="width:150px"
                                       class="filter-item" :readonly="dis">
                                <el-option v-for="item in partRiver" :key="item.riverId" :label="item.riverName"
                                           :value="item.riverId"/>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="5">
                    <el-col :span="12">
                        <el-form-item label="经度" :label-width="formLabelWidth" prop="longitude">
                            <el-input v-model="form.longitude" auto-complete="off" :readonly="dis"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="纬度" :label-width="formLabelWidth" prop="latitude">
                            <el-input  v-model="form.latitude" auto-complete="off" :readonly="dis"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="5">
                    <el-col :span="24">
                        <el-form-item label="部件备注" :label-width="formLabelWidth" prop="newsContents">
                            <el-input type="textarea" v-model="form.partsNote" auto-complete="off"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button size="medium" @click="partsCancel('form')">取 消</el-button>
                <el-button size="medium" type="primary" v-if="isView" @click="partsSubmit('form')">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                addProps: {
                    label:'regName',
                    value:'regId',
                    children: 'children'
                },
                disabledInput:true,
                partType: [],
                partStreet: [],
                partRiver: [],
                tableData: [],
                tableDataAll: [],
                //默认数据总数
                dataCount: 100,
                pageSizes: [5, 10, 20, 30, 40],
                //查询条件
                listQuery: {
                    partType: '', //查询条件存放1
                    belongStreet: '', //查询条件存放2
                    belongRiver: '',//查询条件存放3
                    page: 1,
                    limit: 5
                },
                // chcek: false,//查看时显示,默认隐藏选项
                isView: true,//保存按钮是否显示，默认显示
                listSelect: [],
                dialogFormVisible: false,
                showDialog: false,
                editdialogForm: false,
                loading: false,
                dis:true,
                form: {},
                oneCol: {},
                rules: {
                    partsName: [
                        {required: true, message: '请输入部件名称', trigger: 'blur'},
                    ],
                    partsCode: [
                        {required: true, message: '请输入部件编码', trigger: 'blur'},
                    ],
                    partsType: [
                        {required: true, message: '请输入部件类型', trigger: 'blur'},
                    ],
                    partsNote: [
                        {required: true, message: '请输入部件备注', trigger: 'blur'},
                    ],
                    partsFlow: [
                        {required: true, message: '请输入部件所属街道', trigger: 'blur'},
                    ],
                    riverId: [
                        {required: true, message: '请输入部件所属河段', trigger: 'blur'},
                    ],
                    longitude: [
                        {required: true, message: '请输入部件经度', trigger: 'blur'},
                    ],
                    latitude: [
                        {required: true, message: '请输入部件纬度', trigger: 'blur'},
                    ],
                },
                formLabelWidth: '100px',
            }
        },
        created() {

        },
        mounted() {
            this.getList(); //初始化页面是数据
            this.getTypeOption();//对河道类型下拉框取值
            this.getStreetOption()//对所属街道和所属河段下拉框查询
        },
        computed:{
            treeData:function(){
                return this.$store.state.dropTree
            }
        },
        destroyed() {
        },
        methods: {
            // 状态翻译
            formatState: function (row, column) {
                return row.parts_state == 0 ? "启用" : "停用";
            },
            // 页面刷新
            getList() {
                this.loading = true;
                this.postRequest('/api/rcs/getPartsList', this.listQuery).then((res) => {
                    var data = res.data;
                    console.log('表格数据：',res.data);
                    if (data.status == 200) {
                        this.tableData = data.data;
                        this.dataCount = data.count;
                    } else {
                        this.$message.error(data.msg);
                    }
                    this.loading = false
                }).catch(error => { //错误信息
                    this.$message.error(error);
                })
            },
            getTypeOption() {
                this.getDictionary({types:["PARTIS_TYPE"]}).then(res => {
                    var data = res.data;
                    if (data.status == 200) {
                        this.partType = data.data["PARTIS_TYPE"];
                    } else {
                        this.$message.error(data.msg);
                    }
                }) .catch(error => {
                    //错误信息
                    this.$message.error(error);
                });
            },
            getStreetOption() {
                this.postRequest('/api/rcs/getStreetList').then((res) => {
                    var data = res.data;
                    console.log(data.status == 200);
                    if (data.status == 200) {
                        this.partStreet = data.data[0]; //街道选项
                        // this.partRiver = data.data[1]; //所属河段选项
                        console.log('this.partStreet',this.partStreet);
                    } else {
                        this.$message.error(data.msg);
                    }

                }).catch((error) => {//错误信息
                    console.log(error)
                })
            },
            //级联下拉框change事件
            handleChange(value) {
                this.form.selectedAddress=value
                this.form.regId=value[value.length-1];
                // this.form.regId=value[value.length-1];
                this.disabledInput=false;//释放河段
                //查询区域对应的河段

                this.getRequest('api/rcs/getRiversByRegId?regId='+this.form.regId).then((res) => {
                    var data = res.data;
                    console.log('区域id：',this.form.regId);
                    console.log('区域里面有的河段：',res.data);
                    if (data.status == 200) {
                        this.partRiver =data.data;
                        console.log('partRiver：',this.partRiver );
                    } else {
                        this.$message.error(data.msg);
                    }
                    this.loading = false
                }).catch(error => { //错误信息
                    this.$message.error(error);
                })

            },
            //关闭
            partsCancel(form){
                this.$refs[form].resetFields();
                this.dialogFormVisible = false;
            },
            //保存数据 ->修改 增加
            partsSubmit(form) {
                console.log('form',this.form);
                this.$refs[form].validate(valid => {
                    if (valid) {
                        this.postRequest('/api/rcs/updateParts', this.form).then((res) => {
                            var data = res.data;
                            if (data.status == 200) {
                                this.$message.success(data.msg);
                                this.getList();//list赋值
                            } else {
                                this.$message.error(data.msg);
                            }
                        }).catch((error) => { //错误信息
                            console.log(error)
                        });
                    }
                })
                this.dialogFormVisible = false;
            },
            //批量编辑
            batchEdit(val) {
                if (this.listSelect.length == 0) {
                    this.$message.warning("选中数据");
                    return;
                }
                ;
                var strArr = [];
                for (var i = 0; i < this.listSelect.length; i++) {
                    strArr.push(this.listSelect[i].partsId)
                }
                //前面三个参数分别是 表名，状态字段名，id字段名
                //status 为状态值 （0：正常  9：停用 -1：删除）
                //ids 为需要批量操作的 id数组
                var submitData = {
                    tableName: 'b_river_parts',
                    tableCol: 'parts_state',
                    tableId: 'parts_id',
                    status: val,
                    ids: strArr
                }
                this.putRequest('/api/common/batch', submitData).then((res) => {
                    var data = res.data;
                    if (data.status == 200) {
                        this.$message.success(data.msg);
                        this.getList()
                    } else {
                        this.$message.error(data.msg);
                    }
                }).catch((error) => {
                    //错误信息
                })
            },
            handleSelectionChange(val) {
                this.listSelect = val;
            },
            indexMethod(index) {
                return index + 1;
            },
            //每页个数改变
            handleSizeChange(size) {
                this.listQuery.limit = size;
                this.listQuery.page = 1;
                this.getList()
            },
            //当前页改变
            handleCurrentChange(currentPage) {
                this.listQuery.page = currentPage;
                this.getList()
            },
            //添加
            handleAdd() {
                console.log('this.partType)',this.partType);
                this.dis=false,
                    this.readonly=true,
                    this.chcek = false,//查看时显示,默认隐藏选项
                    this.isView = true, //保存按钮是否显示，默认显示
                    this.dialogFormVisible = true;
                this.form = {};
            },
            //查看操作
            handleView(index, row) {
                this.dis=true,
                    this.readonly=false,
                    this.chcek = true;//查看时显示,默认隐藏选项
                this.isView = false;//保存按钮是否显示，默认显示
                this.dialogFormVisible = true;//打开dialog
                this.form = JSON.parse(JSON.stringify(this.tableData[index]));
                this.form.riverId = row.riverId.code;
                this.form.partsType = row.partsType.code;
            },
            //编辑弹窗
            handleEdit(index, row) {
                this.dis=false,
                    this.readonly=true,
                    this.chcek = false;//查看时显示,默认隐藏选项
                this.isView = true;//保存按钮是否显示，默认显示
                this.dialogFormVisible = true;
                this.form = JSON.parse(JSON.stringify(row));
                this.form.riverId = row.riverId.code;
                this.form.regId = row.regId.code;
                this.form.partsType = row.partsType.code;


            },
            //单行删除
            handleDel(index, row) {
                this.$confirm('确认删除？')
                    .then(_ => {
                        //前面三个参数分别是 表名，状态字段名，id字段名
                        //status 为状态值 （0：正常  9：停用 -1：删除）
                        //ids 为需要批量操作的 id数组
                        var strArr = [];
                        strArr.push(row.partsId);
                        var submitData = {
                            tableName: 'b_river_parts',
                            tableCol: 'parts_state',
                            tableId: 'parts_id',
                            status: '-1',
                            ids: strArr
                        }
                        this.putRequest('/api/common/batch', submitData).then((res) => {
                            var data = res.data;
                            if (data.status == 200) {
                                this.$message.success(data.msg);
                                this.getList()
                            } else {
                                this.$message.error(data.msg);
                            }
                        }).catch((error) => {
                            //错误信息
                        })
                    })
            }
        }
    }
</script>
<style scoped>

</style>
